/**
* @author Meng Zhang
**/

#include "stdafx.h"
#include "BufferController.h"
#include "../mvc/MVC.h"
#include "../TextBuffer/TextBufferFactory.h"
#include "../TextBuffer/TextBuffer.h"
#include "../mfc/CineApp.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

IMPLEMENT_SINGLETON(BufferController)

BufferController::BufferController() {
	WriteLog("Buffer Controller Created\n");
	//textBufferFactory = TextBufferFactory::Instance();
	textBufferFactory = new TextBufferFactory;
	//buffers.clear();
}

//void BufferController::RegisterBuffer( BufferBase* buffer ) {
	//buffers.insert(buffer);
//}

void BufferController::RemoveBuffer( BufferBase* buffer ) {
	/*std::set<BufferBase*>::iterator iter = buffers.find(buffer);
	ASSERT(iter!=buffers.end());
	delete (*iter);
	buffers.erase(iter);*/
	textBufferFactory->ClearBufferSwap((*(TextBuffer*)buffer));

	if (textBufferFactory->DestroyBuffer(buffer))
		return;

	WriteLog("Didn't find buffer %d\n",buffer);
	ASSERT(!"Didn't find buffer!");
}

/*bool BufferController::EnsureSaveModify() {
	std::set<BufferBase*>::iterator iter;
	for (iter = buffers.begin() ; iter != buffers.end() ;) {
		BufferBase *buffer = (*iter++);
		if (buffer->IsTextBuffer() && ((TextBuffer*)buffer)->IsDirty() &&!TextBufferFactory::Instance()->EnsureSave(*(TextBuffer*)buffer)) {//Got an TextBuffer && isDirty
			return false;
		}
	}
	return true;
}*/

bool BufferController::TryDestroy() {
	if (!textBufferFactory->EnsureSaveModify()) {
		return false;
	}

	TextBuffer* emptyBuffer = (TextBuffer*)((CCineApp*)AfxGetApp())->EmptyBuffer();
	textBufferFactory->SaveFile(*emptyBuffer);

	textBufferFactory->Destroy();
	delete textBufferFactory;
	/*std::set<BufferBase*>::iterator iter;
	for (iter = buffers.begin() ; iter != buffers.end() ; ) {
		BufferBase *buffer = *(iter++);
		delete buffer;
	}*/

	//buffers.clear();
	return true;
}

TextBuffer* BufferController::CreateTextBuffer( BufferCreateInfo *info /*= NULL*/ ) {
	return (TextBuffer*)textBufferFactory->CreateBuffer(info);
}

CTextBufferView* BufferController::CreateTextBufferView() {
	return (CTextBufferView*)textBufferFactory->CreateView();
}

void BufferController::SaveFile( TextBuffer* buffer ) {
	textBufferFactory->SaveFile(*buffer);
}

TextBuffer* BufferController::GetEmptyBuffer() {
	return (TextBuffer*)textBufferFactory->CreateBuffer(NULL);
}

bool BufferController::EnsureSave( TextBuffer* buffer ) {
	return textBufferFactory->EnsureSave(*buffer);
}

std::vector<BHND> BufferController::GetTextBufferList() {
	const std::set<TextBuffer*> &buffers = textBufferFactory->TextBuffers();
	std::vector<BHND> textBuffers(buffers.size());
	std::set<TextBuffer*>::const_iterator iter = buffers.begin();
	int i = 0;
	while (iter!=buffers.end()) {
		textBuffers[i++] = (BHND)*(iter++);
	}
	
	return textBuffers;
}

/*TextBuffer* BufferController::GetTextBufferByHandle( int index ) {
	const std::set<TextBuffer*> &buffers = textBufferFactory->TextBuffers();
	std::set<TextBuffer*>::const_iterator iter = buffers.begin();
	while (index>0) {
		iter++;
		--index;
	}
	return *iter;
}*/

TextBuffer* BufferController::GetTextBufferByHandle( BHND bhnd ) {
	const std::set<TextBuffer*> &buffers = textBufferFactory->TextBuffers();
	if (buffers.find((TextBuffer*)bhnd)!=buffers.end())
		return (TextBuffer*)bhnd;
	return NULL;
}

void BufferController::VirtualSaveBuffer( TextBuffer *buffer ) {
	return textBufferFactory->VirtualSave(*buffer);
}